package com.neuedu.web.controller;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/11/1212:27
 */


/**
 * 线程不安全
 *
 *
 *
 *  http 请求   7  get  post  head  options trace delete  PUT
 */
public class SecondServlet extends HttpServlet {


    public SecondServlet() {
        System.err.println("实例化对象");
    }


    @Override
    public void init() throws ServletException {
        // try {
        //     Thread.sleep(5000);
        // } catch (InterruptedException e) {
        //     throw new RuntimeException(e);
        // }


        // ServletConfig servletConfig = getServletConfig();
        // String driverClass = servletConfig.getInitParameter("driverClass");

        String driverClass = super.getInitParameter("driverClass");


        System.out.println("获取的参数  driverClass = " + driverClass);

        System.err.println("SecondServlet.init");
    }

    /**
     * 多线程
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.err.println("SecondServlet.doGet");
        System.out.println("Thread.currentThread().getId() = " + Thread.currentThread().getId());
        System.out.println("this = " + this);



        String numa = req.getParameter("numa");
        String numb = req.getParameter("numb");



        // 具体的业务逻辑
       // 设置ContentType
        resp.setContentType("text/html;charset=utf-8");
        //设置响应编码
        resp.setCharacterEncoding("utf-8");


        //获取输出流

        PrintWriter writer = resp.getWriter();

        //输出
        // writer.write("<h1>第二个Servlet</h1>");
        writer.write("<h2>运算的结果 "+(Integer.parseInt(numa)/ Integer.parseInt(numb))+"  </h2>");
        writer.flush();
        writer.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.err.println("SecondServlet.doPost");
        this.doGet(req, resp);
    }

    @Override
    public void destroy() {
        System.err.println("SecondServlet.destroy");
    }
}
